<?php
	defined( "AZ_EXE_" ) or die ( "ERROR : CORE VARIABLE IS NOT DEFINED." );
	class ComboOptGroup extends Element {
		public $listItemOptGroups = array() ;
		public $listItmesOpts = array();
		public $selectedItem = "" ;
		public $defaultItem = "" ;
		public function __construct( $attributes = array() ,$listItemOptGroup = array(), $listItemOpts = array() , $defaultItem = "" , $selectedItem = "" ){
			parent::__construct("select",$attributes);
			$this->listItemOptGroups = $listItemOptGroup;
			$this->listItemOpts = $listItemOpts ;
			$this->selectedItem = $selectedItem ;
			$this->defaultItem = $defaultItem ;
		}
		public function getCombo( $attributes = array() ,$listItemOptGroup = array(), $listItemOpts = array() , $defaultItem = "" , $selectedItem = "" ){
			return new ComboOptGroup( $attributes, $listItemOptGroup, $listItemOpts , $defaultItem , $selectedItem );
		}
		public function setListItems( $listItems = array() ){
			$this->listItems = $listItems ;
		}
		public function setDefaultItem( $defaultItem = "" ){
			$this->defaultItem = $defaultItem ;
		}
		public function setSelectedItem ( $selectedItem = "" ){
			$this->selectedItem = $selectedItem ;
		}
		public function getElement(){//die(print_r($this->listItemOpts));
			$selectItems="";
			$this->defaultItem != ""?$selectItems = "<option value=\"\" >$this->defaultItem</option>":false;
			
			foreach( $this->listItemOptGroups as $key2 => $value2){
				if(is_array($value2)){
					$selectItems .= "<optgroup label=\"" . $value2[1] . "\">";
					foreach ( $this->listItemOpts AS $key => $value ){
						if($key2 == $value[0]){
							if ( $key == $this->selectedItem ){
								$selectItems .="<option value=\"$key\" selected=\"selected\" >- " . str_replace(";",'',$value[1]) . "</option>" ;
							}
							else {
								$selectItems .="<option value=\"$key\" >- " . str_replace(";",'',$value[1]) . "</option>" ;
							}
						}
					}
				}else{
					$selectItems .= "<optgroup label='" . $value2 . "'>";
					foreach ( $this->listItemOpts AS $key => $value ){
						if($value2 == $value[0]){
							if ( $key == $this->selectedItem ){
								$selectItems .="<option value=\"$key\" selected=\"selected\" >- " . str_replace(";",'',$value[1]) . "</option>" ;
							}
							else {
								$selectItems .="<option value=\"$key\" >- " . str_replace(";",'',$value[1]) . "</option>" ;
							}
						}
					}
				}
				$selectItems .= "</optgroup>";
			}
			parent::setValue( $selectItems );
			return parent::getElement();
		}
	}
?>